package org.apollo.sqlite.sql.query;

import java.util.Arrays;

import org.apollo.sqlite.core.SqliteContext;

public class SqlQuery implements Query {
	
	private StringBuilder sqlBuilder = new StringBuilder();
	
	private String tableName;

	public SqlQuery(String tableName) {
		this.tableName = tableName;
	}

	@Override
	public QueryConst query(boolean distinct, String... args) {
		return query(distinct, Arrays.asList(args));
	}

	@Override
	public QueryConst query(boolean distinct, Iterable<String> args) {
		sqlBuilder.append("SELECT ");
		if (distinct) {
			sqlBuilder.append("DISTINCT ");
		}
		if (args != null && args.iterator().hasNext()) {
			for (String s : args) {
				sqlBuilder.append(SqliteContext.getColumn(s)).append(",");
			}
			sqlBuilder.delete(sqlBuilder.length() - 1, sqlBuilder.length());
		} else {
			sqlBuilder.append("*");
		}
		sqlBuilder.append(" FROM " + SqliteContext.getTable(tableName));
		return new SqlQueryConst(sqlBuilder);
	}

	@Override
	public QueryConst query(String... args) {
		return query(Arrays.asList(args));
	}

	@Override
	public QueryConst query(Iterable<String> args) {
		return query(false, args);
	}
}
